package com.dupf.product.web.controller.product;

import com.dupf.common.base.BaseController;
import com.dupf.common.util.ItvJsonUtil;
import com.dupf.product.common.constant.ProductResult;
import com.dupf.product.common.constant.ProductResultConstant;
import com.dupf.product.dao.model.ProductExchangeRate;
import com.dupf.product.dao.model.ProductExchangeRateExample;
import com.dupf.product.rpc.api.ProductExchangeRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 产品controller
 * Created by dupf on 2018/3/20.
 */
@Controller
@Api(value = "汇率设置", description = "汇率设置")
@RequiresPermissions("system_exchange_rate")
@RequestMapping("/system/exchange-rate")
public class ProductExchangeRateController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductExchangeRateController.class);

    @Autowired
    private ProductExchangeRateService exchangeRateService;

    @ApiOperation(value = "获取汇率设置")
    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public Object exchangeRate(HttpServletRequest request) {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");

        ProductExchangeRateExample productExchangeRateExample = new ProductExchangeRateExample();
        productExchangeRateExample.createCriteria().andOrgIdEqualTo(orgId);
        productExchangeRateExample.setOrderByClause("id DESC");

        ProductExchangeRate productExchangeRate = exchangeRateService.selectFirstByExample(productExchangeRateExample);

        if (productExchangeRate == null) {
            productExchangeRate = new ProductExchangeRate();
            productExchangeRate.setOrgId(orgId);
            productExchangeRate.setCad(new BigDecimal(4.5));
            productExchangeRate.setInr(new BigDecimal(1));
            productExchangeRate.setMxn(new BigDecimal(0.12));
            productExchangeRate.setEur(new BigDecimal(7.2));
            productExchangeRate.setNok(new BigDecimal(1));
            productExchangeRate.setJpy(new BigDecimal(0.027));
            productExchangeRate.setAud(new BigDecimal(1));
            productExchangeRate.setUsd(new BigDecimal(5.9));
            productExchangeRate.setGbp(new BigDecimal(8.2));
            productExchangeRate.setCtime(System.currentTimeMillis());

            exchangeRateService.insertSelective(productExchangeRate);
        }

        return new ProductResult(ProductResultConstant.SUCCESS, productExchangeRate);
    }

    @ApiOperation(value = "更新汇率设置")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws IOException {
        Integer orgId = (Integer) request.getAttribute("currentOrgId");
        String requestStr = super.getInputStream(request);
        JSONObject data = JSONObject.fromObject(requestStr);

        ProductExchangeRate productExchangeRate = ItvJsonUtil.readValue(data.toString(), ProductExchangeRate.class);

        productExchangeRate.setOrgId(orgId);

        int count = exchangeRateService.updateByPrimaryKeySelective(productExchangeRate);

        return new ProductResult(ProductResultConstant.SUCCESS, count);
    }

}